<!--
 * @Author: 刘晟勇
 * @Date: 2021-10-17 10:07:49
 * @LastEditTime: 2021-10-23 17:15:51
 * @Description: 登录组件
-->
<template>
  <div>
    <!-- 头部区域 -->
    <HeaderNav :right="false" class="login-header">
      <template #icon>
        <i class="iconfont icon-left" @click="goBack"></i>
      </template>
      <template #center>
        <span>登录</span>
      </template>
    </HeaderNav>
    <!-- 登录区域 -->
    <section class="page">
      <div class="login-page">
        <!-- 登录框 -->
        <van-form @submit="onSubmit">
          <van-field
            class="login-field"
            v-model="loginFrom.username"
            name="username"
            label="用户名"
            placeholder="用户名/邮箱/已验证手机"
            clearable
            colon
            clear-trigger="always"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="loginFrom.password"
            :type="passwordType"
            name="password"
            label="密码"
            placeholder="请输入密码"
            clearable
            colon
            :right-icon="passwordIcon"
            clear-trigger="always"
            @click-right-icon="toggleType"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
        <!-- 快捷导航 -->
        <div class="quick-nav">
          <router-link tag="span" class="register-button" to="./register"
            >快速注册</router-link
          >
        </div>
        <!-- 其他登录方式 -->
        <div class="other-login">
          <div class="other-head">
            <i></i>
            <span>其它登录方式</span>
            <i></i>
          </div>
          <div class="other-con">
            <!-- @click="qqLogin" -->
            <div class="login-icon" @click="qqLogin">
              <i class="iconfont icon-tubiao215"></i>
              <span>QQ</span>
            </div>
            <div class="login-icon">
              <i class="iconfont icon-weixin1"></i>
              <span>微信</span>
            </div>
          </div>
          <p>登录即代表您已同意<a>知心购隐私政策</a></p>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
// 公共组件的引用
import HeaderNav from "@/components/header-nav.vue";

// 导入混入api方法
import { loginApi } from "@/api/login";

// https://graph.qq.com/oauth2.0/authorize?response_type=coke&client_id=101850878&redirect_uri=http://localhost:8080/proxy&state=1

export default {
  name: "Login",
  components: {
    HeaderNav,
  },
  data() {
    return {
      loginFrom: {
        username: "",
        password: "",
      },
      errorMsg: "",
      passwordType: "password",
      passwordIcon: "a iconfont icon-eye1 eye",
    };
  },
  computed: {
    valideresult() {
      return this.loginFrom.username && this.loginFrom.password;
    },
  },
  methods: {
    // 返回上一级路由
    goBack() {
      this.$router.go(-1);
    },
    onSubmit() {
      loginApi(this.loginFrom, (data) => {
        this.$store.commit("setLoginStatue", {
          token: data.token,
          isLogin: true,
        });
        this.$store.dispatch("getInfo");
        if (this.$route.query.toPath) {
          this.$router.push(this.$route.query.toPath);
        } else {
          this.$router.go(-1);
        }
      });
    },
    toggleType() {
      if (this.passwordIcon === "a iconfont icon-eye1 eye") {
        this.passwordType = "text";
        this.passwordIcon = "a iconfont icon-eye eye";
      } else {
        this.passwordType = "password";
        this.passwordIcon = "a iconfont icon-eye1 eye";
      }
    },
    qqLogin() {
      window.location.href =
        "https://graph.qq.com/oauth2.0/authorize?response_type=token&client_id=101850878&redirect_uri=http://localhost:8080/proxy&state=1";
      // this.$QC.Login.showPopup({
      //   appId: '101850878',
      //   redirectURI:'http://localhost:8080/proxy'
      // })
    },
  },
};
</script>

<style lang="scss" scoped>
// 头部区域
.login-header {
  display: block;
  position: relative;
  width: 100%;
  height: 88px;
  text-align: center;
  line-height: 88px;
  padding: 0 20px;
  font-size: 34px;
  background-color: $bc;
  @include boxSizing;
  .iconfont {
    position: absolute;
    left: 20px;
    top: 0;
    font-size: 50px;
    font-weight: bold;
  }
}
// 登录区域
.page {
  padding: 0 50px;
  width: 100%;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;

  .login-page {
    margin-top: 60px;

    .van-field {
      height: 125px;
      align-items: center;
    }

    .login-wrap {
      width: 100%;

      .login-text {
        @include fj;
        width: 100%;
        height: 60px;
        line-height: 60px;
        padding: 20px 0;
        margin-top: 20px;
        border-bottom: 1px solid #dcdcdc;

        .iconfont {
          font-size: 26px;
          color: #cccccc;

          &.eye {
            padding: 0 30px;
            font-size: 40px;
            border-right: 1px solid #dcdcdc;
          }
        }

        input {
          width: 100%;
          height: 100%;
          margin-right: 20px;
          line-height: 60px;
          color: #222;
          font-size: 32px;

          &.login-password {
            width: 50%;
          }
        }

        span {
          padding-left: 20px;
          font-size: 30px;
        }

        div {
          display: flex;
        }
      }
    }

    .login-error {
      width: 100%;
      height: 40px;
      line-height: 40px;
      padding: 30px 0;
      color: $red;
      font-size: 26px;
    }

    .login-button {
      width: 100%;
      height: 100px;
      text-align: center;
      line-height: 100px;
      color: #fff;
      font-size: 32px;
      background: rgba(246, 53, 21, 0.5);
      @include borderRadius(60px);

      &.active {
        background: rgb(246, 53, 21);
      }
    }

    .quick-nav {
      padding: 40px 0;
      width: 100%;
      text-align: right;

      .register-button {
        color: #999;
        font-size: 28px;
      }
    }

    .other-login {
      width: 100%;
      margin-top: 100px;

      .other-head {
        @include fj;

        i {
          flex: 1;
          height: 1px;
          margin-top: 18px;
          background: #dcdcdc;
        }

        span {
          flex: 1;
          text-align: center;
          font-size: 28px;
          color: #dcdcdc;
        }
      }

      .other-con {
        display: flex;
        width: 320px;
        padding: 40px 0;
        margin: 0 auto;

        .login-icon {
          display: flex;
          flex-direction: column;
          width: 96px;
          margin: 0 30px;
          text-align: center;

          .iconfont {
            width: 100%;
            height: 96px;
            line-height: 96px;
            font-size: 60px;
            color: #15b8f5;
            background: #e7f7fe;
            @include borderRadius(50%);

            &.icon-weixin1 {
              font-size: 50px;
              color: #09bb07;
              background: #e6f8e6;
            }
          }

          span {
            padding-top: 20px;
            color: #999;
          }
        }
      }

      p {
        margin-top: 20px;
        width: 100%;
        text-align: center;
        color: #999;

        a {
          color: #409eff;
        }
      }
    }
  }
}
</style>
